/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * Licensed under Simplified BSD License
 *
 *
 * @author: jin Chen 
 * @date: Nov 30, 2004$
 * @version: 1.0
 */
package edu.psu.geovista.app.pcp.plot.control;

import edu.psu.geovista.app.pcp.plot.control.gui.MiscellaneousPane;
import edu.psu.geovista.app.pcp.plot.control.gui.PlotStrechSection;
import edu.psu.geovista.app.pcp.plot.control.tools.BasicTool;
import edu.psu.geovista.app.pcp.plot.control.tools.PlotTool;
import edu.psu.geovista.app.pcp.plot.layer.AxisLayer;
import edu.psu.geovista.app.pcp.plot.layer.ConfigInfo;
import edu.psu.geovista.app.pcp.toolkits.colorpalette.v2.ColorPalette;
import edu.psu.geovista.app.pcp.toolkits.dialog.FontPanel;
import edu.psu.geovista.app.pcp.toolkits.gui.EvenRowSectionsPane;
import edu.psu.geovista.app.vit.pub.utils.VitUtils;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Hashtable;

public class AppearanceCfg2 extends BasicCfg implements PropertyChangeListener{
    public static final Color DefaultColor_bg=Color.black ;
    public static final Color DefaultColor_axis=Color.yellow ;
    public static final Color DefaultColor_select=Color.red ;

    public static final Font DEFAULT_AXIS_TITLE_FONT=new Font("Arial",Font.PLAIN ,12);


    private String[] colorConfigurees={ ConfigInfo.TARGET_BACKGROUND, ConfigInfo.TARGET_SELECTION, ConfigInfo.TARGET_AXIS };
    EvenRowSectionsPane mainP=new EvenRowSectionsPane();
    PlotStrechSection plotStretchSection;//=new PlotStrechSection();
    ColorPalette colorSection;// =new ColorPalette(colorConfigurees);
    FontPanel axisSection =new FontPanel();
    MiscellaneousPane miscPane;
    public AppearanceCfg2(ConfigManager owner) {
        this.owner=owner;
        this.mainP.setLayout(3,1);
        plotStretchSection=new PlotStrechSection(owner);
        //plotStretchSection.setBorder(new TitledBorder("Plot Stretching"));

        axisSection.setPreviewLocation(SwingConstants.EAST );
        //fontSection.setBorder(new TitledBorder("Axes Font"));

        colorSection =new ColorPalette(colorConfigurees);
        //colorSection.setBorder(new TitledBorder("Color"));

        //first section: Plot stretch + Misc
        EvenRowSectionsPane p=new EvenRowSectionsPane();
        p.clearControlPane();
        p.setLayout(1,2);
        p.addToMainPane(plotStretchSection);
       // 
       //  
       //  



        //2nd section
        EvenRowSectionsPane p3=new EvenRowSectionsPane();
        p3.clearControlPane();
        p3.setLayout(1,2);//1 row. two column
        JPanel datasection = getPcpStringConfigPane();

       // 
      //   
       //  
        // 

         //3rd section
        JPanel p1=new JPanel();
        TitledBorder border = new TitledBorder("Color");
        border.setTitleFont(new Font("Arial",Font.BOLD ,14));
        p1.setBorder(border);
        //p1.setLayout(new BorderLayout());
        p1.add(colorSection);
        this.mainP.addToMainPane(p1 );

         this.setLayout(new BorderLayout() );
         this.add(mainP);
          initValue();
        setControlPane();
    }

    private JPanel getPcpStringConfigPane() {
        EvenRowSectionsPane stringsection =new EvenRowSectionsPane();

        stringsection.clearControlPane();
        stringsection.setLayout(2,1);//1 row. two column
        stringsection.setPreferredSize(new Dimension(200,200));
        /*stringsection.add(new JLabel("String stroke "));
        final JComboBox strokeCmb = getPcpStringStrokeComboBox();*/
        stringsection.setBorder(new TitledBorder("Strings") );
        stringsection.getMainP().setBorder(null);
        stringsection.addToMainPane(this.getPcpStringStrokPane());
        stringsection.addToMainPane(this.getPcpStringAntiAliasPane());
        //stringsection.add(strokeCmb);
        return stringsection;
    }
    private JPanel getPcpStringStrokPane() {
        JPanel p =new JPanel();
        p.add(new JLabel("String stroke "));
        final JComboBox strokeCmb = getPcpStringStrokeComboBox();
        p.add(strokeCmb);
        return p;
    }
    private JComboBox getPcpStringStrokeComboBox() {
        final JComboBox strokeCmb = new JComboBox();
        strokeCmb.setEditable(true);
        strokeCmb.setModel(new DefaultComboBoxModel(new String[] { "0.4", "0.6", "0.8", "1", "1.2", "1.4", "1.6", "1.8", "2", "3", "4" }));
        strokeCmb.setSelectedItem("1");

        strokeCmb.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               actonStrokeChange(strokeCmb.getSelectedItem().toString()  );
           }
        });
        return strokeCmb;
    }
    private JPanel getPcpStringAntiAliasPane() {
        JPanel p =new JPanel();
        //datasection.setPreferredSize(new Dimension(200,200));
        p.add(new JLabel("String antialias "));
        final JComboBox strokeCmb = getPcpStringAntiAliasComboBox();
        p.add(strokeCmb);
        return p;
    }
      private JComboBox getPcpStringAntiAliasComboBox() {
        final JComboBox strokeCmb = new JComboBox();
        strokeCmb.setEditable(true);
        strokeCmb.setModel(new DefaultComboBoxModel(new Boolean[]{Boolean.TRUE, Boolean.FALSE }));
        strokeCmb.setSelectedItem(Boolean.FALSE);
        //strokeFd.setPreferredSize(new Dimension(200,30));

        strokeCmb.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               Boolean panti = (Boolean) strokeCmb.getSelectedItem();
               boolean update = actonAntialiasChange(panti);
               if(!update){//users cancel it
                  strokeCmb.setSelectedItem(Boolean.FALSE);
               }

           }
        });
        return strokeCmb;
    }


    protected void initValue() {
        this.colorSection.addPropertyChangeListener(this);

        TitledBorder border = new TitledBorder("Color");

        /*border.setTitleFont(new Font("Arial",Font.BOLD ,14));
        colorSection.setBorder(border);*/

        this.axisSection.addPropertyChangeListener(this);
        border = new TitledBorder("Axes Font");
        border.setTitleFont(new Font("Arial",Font.BOLD ,14));
        axisSection.setBorder(border);

        border = new TitledBorder("Plot Stretching");
        border.setTitleFont(new Font("Arial",Font.BOLD ,14));
        plotStretchSection.setBorder(border);


        border = new TitledBorder("Miscellaneous");
        border.setTitleFont(new Font("Arial",Font.BOLD ,14));

        reset();
    }

    private void actonStrokeChange(String sstroke) {
        Double aDouble = Double.valueOf(sstroke);
        double vstroke = aDouble.doubleValue();
        Stroke stroke=new BasicStroke((float) vstroke);
        update(ConfigInfo.TARGET_STRING,ConfigInfo.Attr_STROKE ,stroke);
    }
    private boolean actonAntialiasChange(Boolean anti) {
        if(anti.booleanValue() ) {
            String msg="Turn on antialising will make render significantly slower, do you want to continue?";
            int value = VitUtils.showDialog(this, msg, "Confirm", null);
            if(value==JOptionPane.NO_OPTION){

                return false;
            }
        }
        update(ConfigInfo.TARGET_STRING,ConfigInfo.Attr_AntiAlias ,anti );
        return true;
    }

    private void reset(){
        Hashtable defaultColors = this.getDefaultColors();
        this.colorSection.setColortable(defaultColors);

        this.axisSection.setSelectedFont(DEFAULT_AXIS_TITLE_FONT);

        this.plotStretchSection.reset();

    }

    private Hashtable getDefaultColors(){
         Hashtable ct=new Hashtable(3);
         ct.put(ConfigInfo.TARGET_BACKGROUND,this.getSetting(Settings.APPR_BACKGROUND) ); //     DefaultColor_bg
         ct.put(ConfigInfo.TARGET_SELECTION,getSetting(Settings.APPR_SELECT)); //DefaultColor_select
         ct.put(ConfigInfo.TARGET_AXIS,getSetting(Settings.APPR_AXIS_COLOR));   //DefaultColor_axis
        return ct;
    }

    private void setControlPane(){
        this.mainP.clearControlPane();;
        JButton resetBtn=new JButton("Reset");
                    resetBtn.addActionListener(new ActionListener(){
                            public void actionPerformed(ActionEvent e){
                                reset();
                            }
                    });

        JButton closeBtn=new JButton("Close");
                    closeBtn.addActionListener(new ActionListener(){
                            public void actionPerformed(ActionEvent e){
                                  owner.close();
                            }
                    });

        mainP.addToControlPane(resetBtn);
        mainP.addToControlPane(closeBtn);

    }
    //Listen to colorPalatte
    public void propertyChange(PropertyChangeEvent evt) {
            Object o=evt.getSource();
            if(o instanceof edu.psu.geovista.app.pcp.toolkits.colorpalette.v2.ColorPalette){
                PlotTool plotCmd=(PlotTool)owner.getCmd(BasicTool.PlotT );
                String prop=evt.getPropertyName() ;
                if(prop.equalsIgnoreCase("ColorReset") ){ //reset default color

                    plotCmd.setAConfigInfo("background",ConfigInfo.Attr_COLOR,getSetting(Settings.APPR_BACKGROUND));   //DefaultColor_bg
                    plotCmd.setAConfigInfo("axis",ConfigInfo.Attr_COLOR,getSetting(Settings.APPR_AXIS_COLOR));   //DefaultColor_axis
                    plotCmd.setAConfigInfo("selection",ConfigInfo.Attr_COLOR,getSetting(Settings.APPR_SELECT));    //DefaultColor_select


                }
                else{
                String target=evt.getPropertyName() ;
                Object newvalue= evt.getNewValue() ;
                if(newvalue instanceof Color) {
                    Color color=(Color) newvalue;

                  if(plotCmd!=null)  plotCmd.setAConfigInfo(target,ConfigInfo.Attr_COLOR ,color ) ;

                }
                }

            }//
            else if (o instanceof FontPanel){
                String prop=evt.getPropertyName() ;

                if(prop.equalsIgnoreCase("font") ){
                    Font f=(Font) evt.getNewValue();
                    if(f!=null) {

                        this.update("axis",AxisLayer.AxisLayerConfigInfo.YAxisTitleFont,f);
                    }

                }
            }
       }

    private void update(String target,String key,Object value){
         PlotTool plotCmd=(PlotTool)owner.getCmd(BasicTool.PlotT );
         if(plotCmd!=null)plotCmd.setAConfigInfo(target,key,value ) ;
     }

    public PlotStrechSection getPlotStretchSection() {
        return plotStretchSection;
    }


}
